﻿using Microsoft.SharePoint.Administration;
using SharePoint.Security.ConfigAnalyzer.Engine.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace SharePoint.Security.ConfigAnalyzer.Engine
{
    public class RuleEngine
    {
        public List<Base.BaseRuleSet> GetRuleSets()
        {

            var ruleSets = new List<SharePoint.Security.ConfigAnalyzer.Engine.Base.BaseRuleSet>();
            
            var types = from t in Assembly.GetExecutingAssembly().GetTypes()
                    where t.IsSubclassOf(typeof(Base.BaseRuleSet))
                    select t;


            foreach (var type in types)
            {
                if (type.Name == "WebApplicationRuleSet")
                {
                    var webApps = Helper.GetWebApps();

                    foreach (var webApp in webApps)
                    {
                        var instance = (Base.BaseRuleSet)Activator.CreateInstance(type, webApp);
                        ruleSets.Add(instance);
                    }
                }
                else
                {
                    var instance = (Base.BaseRuleSet)Activator.CreateInstance(type);
                    ruleSets.Add(instance);
                }               
            }

            return ruleSets;
        }
        
    }
}
